<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%--第一步--%>
    <!-- 获取CSRF Token -->
    <meta name="_csrf" content="${_csrf.token}"/>
    <!-- 获取CSRF头，默认为X-CSRF-TOKEN -->
    <meta name="_csrf_header" content="${_csrf.headerName}"/>

    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/css/layui.css"   media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/layuimini.css?v=2.0.4.2" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/themes/default.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/public.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui_ext/dtree/dtree.css"><link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui_ext/dtree/font/dtreefont.css">
</head>
<body>
    <div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-row">
            <%-- 左侧菜单树 --%>
            <div class="layui-col-md2">
                <!-- 树节点容器开始 -->
                <ul id="menuTree" class="dtree" data-id="0" style="width: 240px;"></ul>
                <!-- 树节点容器结束 -->
            </div>

            <%-- 右侧数据表格 --%>
            <div class="layui-col-md10">

                <%-- 表格工具栏 --%>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加 </button>
                        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="search"><i class="layui-icon layui-icon-search"></i>查询全部</button>
                    </div>
                </script>

                <%-- 数据表格 --%>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <%-- 行工具栏 --%>
                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-close"></i>删除</a>
                </script>


                <%-- 添加和修改窗口 --%>
                <div style="display: none;padding: 5px" id="addOrUpdateWindow">
                        <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                            <%-- 菜单编号 --%>
                            <input type="hidden" name="id">
                            <div style="display: inline-block" class="layui-form-item">
                                <label class="layui-form-label">父级菜单</label>
                                <div class="layui-input-block">
                                    <%-- 父菜单编号 --%>
                                    <input type="hidden" name="pid" id="pid">
                                    <ul id="menuSelectTree" class="dtree" data-id="0"></ul>
                                </div>
                            </div>
                                <button style="display: inline-block" id="resetMenu" type="button" class="layui-btn layui-btn-danger"><span class="layui-icon layui-icon-return"></span>重置菜单</button>
                            <div class="layui-form-item">
                                <label class="layui-form-label">菜单名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入菜单名称" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item" id="hrefCodeDiv">
                                <label class="layui-form-label">菜单地址</label>
                                <div class="layui-input-block">
                                    <input type="text" name="href"  autocomplete="off" placeholder="请输入菜单地址" class="layui-input">
                                </div>
                            </div>
                            <%--初始化图标选择器--%>
                            <div class="layui-form-item">
                                <label class="layui-form-label">菜单图标</label>
                                <div class="layui-input-block">
                                    <%-- 添加隐藏域，保存选中的图标ID --%>
                                    <input type="hidden" name="icon" id="icon" value="fa fa-star">
                                    <input type="text" name="iconFa" id="iconPicker" lay-filter="iconPicker" autocomplete="off" class="layui-input"/>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">是否展开</label>
                                    <div class="layui-input-block">
                                        <input type="radio" name="spread" value="1" title="是">
                                        <input type="radio" name="spread" value="0" title="否" checked>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">菜单类型</label>
                                    <div class="layui-input-block">
                                        <input type="radio" name="type" value="menu" title="菜单" checked lay-filter="checkPermission">
                                        <input type="radio" name="type" value="permission" title="权限" lay-filter="checkPermission">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item" style="display: none" id="permissionCodeDiv">
                                <label class="layui-form-label">权限编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="permissionCode"  autocomplete="off" placeholder="请输入权限编码" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-form-item layui-row layui-col-xs12">
                                <div class="layui-input-block" style="text-align: center;">
                                    <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                                    <button type="reset" class="layui-btn layui-btn-warm clearBtn"><span class="layui-icon layui-icon-refresh-1"></span>清空</button>
                                </div>
                            </div>
                        </form>
                    </div>
            </div>
        </div>
    </div>
</div>

</body>

<%-- 导入layui的js文件--%>
<script src="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.extend({
        dtree:"${pageContext.request.contextPath}/statics/layui_ext/dtree/dtree"
        ,iconPickerFa:"${pageContext.request.contextPath}/statics/layui/js/lay-module/iconPicker/iconPickerFa"
    }).use(['form','jquery','table','layer','dtree','iconPickerFa'],function() {
        var form = layui.form;
        var $ = layui.jquery;
        var table = layui.table;
        var layer = layui.layer;
        var dtree = layui.dtree;
        var iconPickerFa = layui.iconPickerFa;

        //2 security 中使用post提交
        //获取<meta>标签中封装的CSRF Token
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        //将头中的CSRF Token信息进行发送
        $(document).ajaxSend(function (e,xhr,options) {
            xhr.setRequestHeader(header,token);
        });

        //渲染左侧菜单树
        var menuTree = dtree.render({
            elem: "#menuTree",//绑定ul标签的ID属性值
            url: "/permission/loadMenuTree", //请求地址
            dataStyle: "layuiStyle", //使用layui风格的数据格式
            dataFormat: "list", //配置data的风格为list
            response:{message:"msg",statusCode:0}, //修改response中返回数据的定义
        });
        //渲染添加面板左侧菜单树
        var menuSelectTree = dtree.renderSelect({
            elem: "#menuSelectTree",//绑定ul标签的ID属性值
            url: "/permission/loadMenuTree", //请求地址
            dataStyle: "layuiStyle", //使用layui风格的数据格式
            dataFormat: "list", //配置data的风格为list
            response:{message:"msg",statusCode:0}, //修改response中返回数据的定义
        });

        //树形菜单点击事件
        dtree.on("node('menuTree')" ,function(obj){
            tableIns.reload({
                where:{"id":obj.param.nodeId},//其中nodeId是当前选中节点的id值
            });
        });
        //添加修改树形菜单点击事件
        dtree.on("node('menuSelectTree')" ,function(obj){
            $('#pid').val(obj.param.nodeId);
        });

        //渲染表格组件
        var tableIns = table.render({
            elem: '#currentTableId',
            url: '${pageContext.request.contextPath}/permission/list',
            toolbar: '#toolbarDemo',
            cols: [[
                {field: 'id', title: '菜单编号', align: "center"},
                {field: 'pid',hide:true},
                {field: 'title', title: '菜单名称', align: "center"},
                {field: 'type', title: '菜单类型', align: "center"},
                {field: 'href', title: '菜单地址', align: "center"},
                {field: 'icon', title: '菜单图标', align: "center",templet:function (b) {return "<i class='"+ b.icon +"'></i>";}},
                {field: 'spread', title: '是否折叠', align: "center",templet:function (b) {return b.spread == 1 ? '<font color="green">是</font>' : '<font color="red">否</font>';}},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
            done: function (res, curr, count) {
                //判断当前页码是否是大于1并且当前页的数据量为0
                if (curr > 1 && res.data.length == 0) {
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr: pageValue}
                    });
                }
            }
        });
        //初始化图标选择器组件
        iconPickerFa.render({
            // 选择器，推荐使用input
            elem: '#iconPicker',
            // fa 图标接口
            url: "/statics/layui/lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {
                //给图标隐藏域赋值
                $("#icon").val("fa "+data.icon);
            },
            //监听单选按钮点击事件
            //监听表单提交事件
            //重置菜单按钮
            // 渲染成功后的回调
            success: function (d) {
            }
        });

        //表头添加数据
        //定义变量 上传和修改的url、上传和修改窗口的索引
        let url,mainIndex;
        table.on('toolbar(currentTableFilter)',function (obj) {//lay-filter属性
            switch (obj.event) {
                case 'add':
                    mainIndex = layer.open({
                        type:1,
                        title:"添加菜单",
                        area: ["700px", "500px"],//窗口宽高
                        content: $("#addOrUpdateWindow"),//引用的内容窗口
                        success:function () {
                            //提交地址
                            url = "/permission/add";
                            //清空表单数据
                            $("#dataFrm")[0].reset();
                            //设置默认图标
                            iconPickerFa.checkIcon('iconPicker','fa fa-star');
                            //权限编码默认隐藏
                            $('#permissionCodeDiv').hide();
                        }
                    });

                    break;
                case 'search':
                    menuTree.reload();
                    tableIns.reload({
                        where:{"id":''},
                    });
                    break;
            }
        });

        //监听表单提交事件
        form.on("submit(doSubmit)",function (data) {
            $.post(url,data.field,function(result){
                if(result.success){
                    //刷新数据表格
                    tableIns.reload();
                    //关闭窗口
                    layer.close(mainIndex);
                    //刷新左侧菜单树
                    menuTree.reload();
                    //刷新下拉菜单树
                    menuSelectTree.reload();
                    layer.msg(result.message,{icon:1});
                }else{
                    layer.msg(result.message,{icon:2});
                }
            },"json");
            //禁止页面刷新
            return false;
        });

        //监听菜单类型单选按钮的点击事件
        form.on("radio(checkPermission)",function (data) {
            if(data.value=="permission"){
                $("#permissionCodeDiv").show();//显示权限编码区域
            }else{
                $("#permissionCodeDiv").hide();//隐藏
            }
        });
        //重置下拉菜单
        $("#resetMenu").click(function () {
            $('#pid').val('');
            menuSelectTree.selectResetVal();
        });
        $('.clearBtn').click(function () {
            iconPickerFa.checkIcon('iconPicker','fa fa-star');
            $("#permissionCodeDiv").hide();//隐藏
        })

        //行编辑修改和删除
        table.on('tool(currentTableFilter)',function (obj) {//与表格的lay-filter属性绑定
            let rowData = obj.data;
            switch (obj.event) {
                case 'edit':
                    mainIndex = layer.open({
                        type : 1, //弹出层类型
                        title : '编辑菜单',
                        area: ["700px", "500px"],//窗口宽高
                        content : $('#addOrUpdateWindow'),//面板内容
                        success : function () {
                            //给修改表单赋值
                            form.val('dataFrm',rowData);
                            //回显下拉数的数据
                            dtree.dataInit('menuSelectTree',rowData.pid);
                            dtree.selectVal('menuSelectTree');
                            //如果当前选中节点是一级菜单 则没有父节点 默认显示请选择
                            if(rowData.pid == 0){
                                menuSelectTree.reload();
                            }
                            //icon回显
                            iconPickerFa.checkIcon('iconPicker',rowData.icon);
                            //设置表单提交的url
                            url = '/permission/update';
                            if(rowData.type == 'menu'){
                                $('#permissionCodeDiv').hide();
                            }else{
                                $('#permissionCodeDiv').show();
                            }
                        }
                    });
                    break;
                case 'delete':
                    $.post("/role/checkRoleByUser",{roleId:rowData.id},function (result) {
                        layer.confirm("确定删除该菜单及该菜单所占的角色菜单表中的信息吗？",{icon:3,title:'请确认'},function (index) {
                            $.post("/permission/del",{permissionId:rowData.id},function (result) {
                                if(result.success){
                                    layer.msg(result.message,{icon:1});
                                    //刷新数据表格
                                    tableIns.reload();
                                    //刷新左侧菜单树
                                    menuTree.reload();
                                    //刷新下拉菜单树
                                    menuSelectTree.reload();
                                }else{
                                    layer.msg(result.message,{icon:2});
                                }
                            },'json');
                            layer.close(index);
                        });
                    },'json');
                    break;
            }
        });
    });
</script>
</html>
